package com.ib.review.base.utils;

import io.minio.*;
import org.springframework.web.multipart.MultipartFile;

public class MinioService {
    private final MinioClient minioClient;

    public MinioService(String endpoint,String accessKey,String secretKey) {
        this.minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    public void uploadFile(MultipartFile file, String bucketName, String folderPath, String fileNameUuid)
            throws Exception {
        // 检查 Bucket 是否存在
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder()
                .bucket(bucketName)
                .build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }

        // 上传文件
        String objectName = folderPath + "/" + fileNameUuid;
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(file.getInputStream(), file.getSize(), -1)
                        .contentType(file.getContentType())
                        .build());
    }
}